package com.mcptt.db.model;

import java.io.Serializable;

import com.cloudptt.api.product.log.Log;
import com.cloudptt.api.product.res.Resource;
import com.mcptt.config.model.McIdInfo;
import com.mcptt.config.model.ServerInfo;

public class AddrInfo implements Serializable
{   
	private static final String TAG = "AddrInfo";
	public static final String KEY = "addrInfo";
	public static final String KEY_CUR = "cur";
	public static final String Sys_Num = "0000000000";
	
	public static final int GroupType_Default = 1;
	public static final int GroupType_Normal = 2;
	public static final int GroupType_Incomplete = 9;
	
	public static final int UserType_Sys = 10;
	public static final int UserType_Dac = 11;
	public static final int UserType_User = 12;
	public static final int UserType_Camera = 13;
	
	public static final int UserType_Ai_Car = 15;
	public static final int UserType_Ai_Plane = 16;
	public static final int UserType_Default = 19;
	
	private static String SvrAddrDesc = ServerInfo.Default_Svr_Ip +":"+ ServerInfo.Default_Svr_Port;

//	public static final int McType_Ptt = 1;
//	public static final int McType_Video = 2;
//	public static final int McType_Data = 4;
//	public static final int McType_All = McType_Ptt + McType_Video + McType_Data;
//	public static final int McType_None = 0;




	//	private static final String Default_CountryCode = "0086";
	protected int type;
	protected String mcId;

	protected String name;

	protected int level = 0;
//	private String groupId;
//	private String mcvideoId;
//	private String mcdataId;
//	private int attr = 0;
//	private int userCount = 0;
//	private int onlineState = 0;
//	private List<UserInfo> userInfo = null;

	//	private boolean isInCall = false;;



//	public boolean isDefaultInfo()
//	{
//		return this.userType == UserType_Group && 
//				name.equals("") && groupId.equals("");
//	}
	public static void setSvrAddrDesc(String svrIp,int port) {
		SvrAddrDesc = svrIp + ":" + port;
	}
	
	public AddrInfo() 
	{
		
	}
	public AddrInfo(int type,String name,String mcId,int level)
	{
		this.type = type;
		this.mcId = mcId;
		if(name == null
				|| name.trim().length() <= 0)
		{
			this.name = mcId;
		}
		else
		{
			this.name = name;
		}
		this.level = level;
	}


	
	public boolean isGroupType()
	{
		if(type == AddrInfo.GroupType_Default
				|| type == AddrInfo.GroupType_Normal
				|| type == AddrInfo.GroupType_Incomplete)
		{
			return true;
		}
		return false;
	}

	public boolean isUserType()
	{
		if(type == AddrInfo.UserType_Sys
				|| type == AddrInfo.UserType_Dac
				|| type == AddrInfo.UserType_User)
		{
			return true;
		}
		return false;
	}
	public int getType() {
		return type;
	}
	
	public void setType(int userType) {
		this.type = userType;
	}

	public String getName()
	{
		return name;
	}
	public void setName(String name)
	{
		this.name = name;
	}

	public int getLevel()
	{
		return level;
	}

	public void setMcId(String mcId) {
		this.mcId = mcId;
	}

	public String getMcId() {
		return mcId;
	}
	
	public String getDscp()
	{
		if(name != null
				&& name.length() > 0)
		{
			return name;
		}
		if(mcId != null
				&& mcId.length() > 0)
		{
			return mcId;
		}
		else
		{
			return "";
		}
	}
	public static String getDefaultName(String mcId)
	{
		String sysName = Resource.getString("sys_name");
		UserInfo info = new UserInfo(UserType_Sys,sysName,UserInfo.Sys_Num,"3000");
		return "user-" + mcId.substring(mcId.length() -2);
	}
	public String getDefaultName()
	{
		String sysName = Resource.getString("sys_name");
		UserInfo info = new UserInfo(UserType_Sys,sysName,UserInfo.Sys_Num,"3000");
		return "user-" + mcId.substring(mcId.length() -2);
	}
	public boolean isSys() {
		if(type == UserType_Sys)
		{
			return true;
		}
		return false;
	}
	public boolean isDac() {
		if(type == UserType_Dac)
		{
			return true;
		}
		return false;
	}
	public boolean isCamera() {
		if(type == UserType_Camera)
		{
			return true;
		}
		return false;
	}
	public boolean isAi() {
		if(type == UserType_Ai_Car
				|| type == UserType_Ai_Plane)
		{
			return true;
		}
		return false;
	}
	public boolean isSameOne(AddrInfo oldInfo)
	{
		if(oldInfo == null)
		{
			return false;
		}
		String oldMcId = oldInfo.mcId;
		if(this.mcId != null
				&& this.mcId.equals(oldMcId))
		{
			return true;
		}
		return false;
	}

	public boolean isSameOne(UserInfo oldInfo)
	{
		if(oldInfo == null)
		{
			return false;
		}
		String oldMcId = oldInfo.mcId;
		if(this.mcId.equals(oldMcId))
		{
			return true;
		}
		return false;
	}

	public boolean isSameOne(McIdInfo mcIdInfo)
	{
		if(mcIdInfo == null)
		{
			return false;
		}
		String oldNum = mcIdInfo.getNum();
		if(this.mcId.equals(oldNum))
		{
			return true;
		}
		return false;
	}

	public boolean isSameOne(String value)
	{
//		Log.d(TAG, "isSameOne value = " + value + " mcId = " + mcId + " name = " + name);
		if(this.mcId.equals(value))
		{
			return true;
		}
		else if(this.name.equals(value))
		{
			return true;
		}
		return false;
	}

	public boolean isNameSameOne(String value)
	{
//		Log.d(TAG, "isNameSameOne value = " + value  + " name = " + name);
		if(this.name.equals(value))
		{
			return true;
		}
		return false;
	}


	public String toString()
	{
		StringBuffer strBuf = new StringBuffer();
		strBuf.append("userType = ").append(type).append(",");
		strBuf.append("mcId = ").append(mcId).append(",");
		strBuf.append("name = ").append(name).append(".");


		return strBuf.toString();
	}


	protected static final String BeginKey = "sip:";
	protected static final String EndKey = "@";
	public static String toNumFromSipUrl(String urlStr)
	{
		Log.d(TAG, "convertSipUrlToNum urlStr = " + urlStr);
		if(urlStr == null
				|| urlStr.length() <= 0)
		{
			return "";
		}
		int beginIndex = urlStr.indexOf(BeginKey);
		int endIndex = urlStr.indexOf(EndKey);
		if(beginIndex == -1
				|| endIndex == -1
				|| beginIndex > endIndex)
		{
			return "";
		}
		beginIndex = beginIndex + BeginKey.length();
		urlStr = urlStr.substring(beginIndex, endIndex);
//		urlStr = urlStr.substring(1, urlStr.length());
		Log.d(TAG, "convertSipUrlToNum end urlStr = " + urlStr);
		return urlStr;//去掉前面�?0
	}
	
	public static String toSipUrlFromNum(String num)
	{
		StringBuffer strBuf = new StringBuffer("<");
		strBuf.append(BeginKey).append(num).append(EndKey).append(SvrAddrDesc).append(">");
		return strBuf.toString();
	}
	public static String parseUserFromTakenStr(String str)
	{
		Log.d(TAG, "parseUserFromTakenStr str = " + str);
		if(str == null
				|| str.length() <= 0)
		{
			return "";
		}
		int len = str.length();
		int beginIndex = str.lastIndexOf("/");
		beginIndex = beginIndex + 1;
		if(beginIndex <= 0
				|| beginIndex > len)
		{
			return "";
		}
		int endIndex = str.lastIndexOf("@");
		if(endIndex <= 0
				|| beginIndex > len)
		{
			return "";
		}
		if(beginIndex >= endIndex)
		{
			return "";
		}
		str = str.substring(beginIndex, endIndex);
		Log.d(TAG, "parseUserFromTakenStr end str = " + str);
		return str;
	}
}
